package com.grasp.activity.scene;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.grasp.activity.BaseFragment;
import com.grasp.activity.R;
import com.grasp.base.ActionBar;
import com.grasp.base.ActionBarOnClick;
import com.grasp.base.CustomGridViewAdapter;
import com.grasp.base.CustomItemData;
import com.grasp.data.IconItem;
import com.grasp.util.IntentUtil;

public class SceneMainFragment extends BaseFragment implements
		OnItemClickListener, OnItemLongClickListener {

	private CustomGridViewAdapter mAdapter;
	private GridView mGridView;
	private List<IconItem> items;

	@Override
	protected void initView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater
				.inflate(R.layout.activity_main_scene, container, false);
		initActionBar();
		mGridView = (GridView) mView.findViewById(R.id.scene_gridview);
	}

	@Override
	protected void main() {
		items = new ArrayList<IconItem>();
		IconItem item = new IconItem();
		item.setTitle("添加场景");
		item.setFileName("icon_add");
		items.add(item);
		mAdapter = new CustomGridViewAdapter(getActivity(), items);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(this);
		mGridView.setOnItemLongClickListener(this);
	}

	private void initActionBar() {
		ActionBar mActionBar = (ActionBar) mView
				.findViewById(R.id.scene_actionbar);
		mActionBar.setActionBarOnClickListener(new ActionBarOnClick() {

			@Override
			public void rightBtnOnClick() {
				Toast.makeText(getActivity(), "rightBtnOnClick",
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void leftBtnOnClick() {
				Toast.makeText(getActivity(), "leftBtnOnClick",
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg2 == items.size() - 1) {
			IntentUtil.startActivity(getActivity(), AddSceneActivity.class);
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		if (arg2 != items.size() - 1)
			Toast.makeText(getActivity(), "item " + arg2 + ", " + arg3,
					Toast.LENGTH_SHORT).show();
		return false;
	}
}
